package tuiLayer;
import ControlLayer.*;
import java.util.Scanner;
import ModelLayer.*;

/**
 * Write a description of class PersonUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonUI
{
    private PersonCtr personCtr;
    private String command;

    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI()
    {
        personCtr = new PersonCtr();
    }

    private String inputPersonName()
    {
        Scanner a = new Scanner(System.in);
        System.out.println();
        System.out.println("Type the person's name: ");
        String name = a.nextLine();
        return name;
    }

    private String inputAddress()
    {
        Scanner a = new Scanner(System.in);
        System.out.println("Type in the address: ");
        String address = a.nextLine();
        return address;
    }

    private String inputZip()
    {
        Scanner a = new Scanner(System.in);
        System.out.println("Type in the zip number: ");
        String zip = a.nextLine();
        return zip;
    }

    private String inputCity()
    {
        Scanner a = new Scanner(System.in);
        System.out.println("Type in the name of the city: ");
        String city = a.nextLine();
        return city;
    }

    private String inputPhone()
    {
        Scanner a = new Scanner(System.in);
        System.out.println("Type in the phone number: ");
        String phone = a.nextLine();
        return phone;
    }

    private void createPerson()

    {
        String name = inputPersonName();
        String address = inputAddress();
        String zip = inputZip();
        String city = inputCity();
        String phone = inputPhone();
        System.out.println("\f");
        System.out.println("* New Person created. *");

        personCtr.createPerson(name, address, zip, city, phone);
    }

    public void personMenu()
    {
        boolean finished = false;
        while (!finished)
        {
            printMenu();
            reader();
            if(command.equals("1"))
            {
                System.out.println("Returning to main menu.");
                finished = true;
            }
            else if(command.equals("help"))
                ;
            else if(command.equals("2"))
                createPerson();
            else if(command.equals("3"))
                update();
            else if(command.equals("4"))
                delete();
            else if(command.equals("5"))
                listOfPeople();     
        }
    }

    private void printMenu()
    {
        System.out.println();
        System.out.println("*** Person Menu ***");
        System.out.println("(1) Return to Main menu.");
        System.out.println("(2) Create new Person.");
        System.out.println("(3) Update Person.");
        System.out.println("(4) Delete Person.");
        System.out.println("(5) List People.");
        System.out.println("Please select using the keypad");
    }

    private void reader()
    {
        final String[] validCommands = {"1","2","3","4","5","6","help",};
        Scanner reader;
        reader = new Scanner(System.in);
        boolean finished = false;
        while(!finished)
        {
            System.out.println();
            System.out.print("> ");
            String terminal = reader.nextLine();
            System.out.println();
            for(int i = 0; i < validCommands.length; i++) 
            {
                if(validCommands[i].equals(terminal))
                {
                    command = terminal;
                    finished = true;
                }
            }
            if(finished == false)
                System.out.print("Try once again.");
        }
    }

    private String reader2()
    {
        Scanner terminal = new Scanner(System.in);
        System.out.print(">> ");
        String command2 = terminal.nextLine();
        return command2;
    }

    private void update()
    {
        boolean found = false;
        System.out.println("Type in the Person's name:");
        String nameSearch = reader2();
        if(personCtr.findPerson(nameSearch) !=null)
        {
            boolean finished = false;
            while(!finished)
            {
                System.out.println();
                System.out.println("*** Person Update Menu ***");
                System.out.println("(1) Return to Person menu.");
                System.out.println("(2) Change Name.");
                System.out.println("(3) Change Address.");
                System.out.println("(4) Change City.");
                System.out.println("(5) Change Zip.");
                System.out.println("(6) Change Phone number.");
                System.out.println("Please select using the keypad");
                reader();
                if(command.equals("1"))
                {
                    System.out.println("Returning to Person menu.");
                    finished = true;
                }
                else if(command.equals("2"))
                {
                    System.out.println("Now, type in the new name:");
                    String newName = reader2();
                    personCtr.updateName(nameSearch, newName);
                    finished = true;
                }
                else if(command.equals("3"))
                {
                    System.out.println("Now, type in the new address:");
                    String newAddress = reader2();
                    personCtr.updateAddress(nameSearch, newAddress);
                }
                else if(command.equals("4"))
                {
                    System.out.println("Now, type in the new city:");
                    String newCity = reader2();
                    personCtr.updateCity(nameSearch, newCity);
                }
                else if(command.equals("5"))
                {
                    System.out.println("Now, type in the new zip:");
                    String newZip = reader2();
                    personCtr.updateZip(nameSearch, newZip);
                }
                else if(command.equals("6"))
                {
                    System.out.println("Now, type in the new phone number:");
                    String newPhone = reader2();
                    personCtr.updatePhone(nameSearch, newPhone);
                }
            }
        }
        else
        {
            System.out.println("\f");
            System.out.println("################");
            System.out.println("Person not found");
            System.out.println("################");
        }
    }

    private void delete()
    {
        System.out.println("Type in the Person's name:");
        String personToDelete = reader2();
        boolean finished = false;
        if(personCtr.findPerson(personToDelete) !=null)
        {
            System.out.println("Do you want to delete?");
            System.out.println("Type in: (1)yes or (2)no");
            reader();
            if(command.equals("1"))
            {
                personCtr.deletePerson(personToDelete);
                System.out.println("\f");
                System.out.println("* Person was deleted *");
                finished = true;
            }
            else if(command.equals("2"))
            {
                System.out.println("\f");
                System.out.println("* Person wasn't deleted *");
            }
        }
    }

    private void listOfPeople()
    {
        System.out.println("\f");
        for (Person PersonObj: personCtr.getList())
        {
            System.out.println("--------------------------");
            System.out.println("Name: " + PersonObj.getName());
            System.out.println("Address: " + PersonObj.getAddress());
            System.out.println("City: " + PersonObj.getCity());
            System.out.println("Zip: " + PersonObj.getZip());
            System.out.println("Phone number: " + PersonObj.getPhone());
            System.out.println("--------------------------");
        }
    }
}